const { Service } = require('egg');

class AdminService extends Service {
  // 获取数据库中所有的酒店数据
  async getHotelInfoList(page, pageSize) {
    try {
      // 如果有分页执行此语句，
      if (page && pageSize) {
        const HotelInfoList = await this.app.model.Hotel.findAll({
          offset: (page - 1) * pageSize, // 查询的起始位置
          limit: Number(pageSize), // 限制返回的数据量 转换成整数u传给数据库否则会报错
        }, {
          include: [
            {
              model: this.ctx.model.HotelRoom,
            },
          ],
        });
        return HotelInfoList;
      }
      // 不带分页执行;
      const HotelInfoList = await this.app.model.Hotel.findAll({
        include: [
          {
            model: this.ctx.model.HotelRoom,
          },
        ],
      });
      // 判断有没有数据
      if (HotelInfoList) {
        this.ctx.body = {
          code: 200,
          data: HotelInfoList,
        };
      } else {
        this.ctx.body = {
          code: 400,
          data: '数据获取失败，请联系管理员',
        };
      }
      return HotelInfoList;
    } catch (error) {
      // 获取失败
      return error;
    }
  }
  // 通过酒店的城市id外键进行查询所有信息
  async getHotelInfoListByForgienKeyId(id) {
    try {
      console.log('经历了服务层');

      const HotelInfoListByforgignKey = await this.app.model.Hotel.findAll({
        where: {
          city_id: id,
        },
      });
      // 判断有没有数据
      if (HotelInfoListByforgignKey) {
        this.ctx.body = {
          code: 200,
          data: HotelInfoListByforgignKey,
        };
      } else {
        this.ctx.body = {
          code: 400,
          data: '数据获取失败，请联系管理员',
        };
      }
      return HotelInfoListByforgignKey;
    } catch (error) {
      // 获取失败
      return error;
    }
  }
  // 通过酒店id获取信息
  async getHotelInfoById(id) {
    const hotelById = await this.app.model.Hotel.findAll({
      where: {
        hotel_id: id,
      },
      include: [
        {
          model: this.ctx.model.HotelRoom,
        },
      ],
    });
    return hotelById;
  }
  async addHotel(data) {
    try {
      const { app } = this;
      const addHotel = await app.model.Hotel.create(data);
      return addHotel;
    } catch (error) {
      return {
        code: 500,
        msg: '服务器错误',
        error,
      };
    }
  }
  async updateHotel(id, data) {
    const { app } = this;
    try {
      const hotel_name = data.title;
      const hotel_img = data.cover_img;
      const hotel_price = data.price;
      const hotel_address = data.address;
      const hotel_comment = data.comment;
      const phone = data.phone;
      const updateHotel = await app.model.Hotel.findOne({ where: { hotel_id: id } });
      // console.log(updateHotel);
      if (updateHotel) {
        const update = await updateHotel.update({ hotel_name, hotel_img, hotel_price, hotel_address, hotel_comment, phone });
        return update;
      }
    } catch (error) {
      return error;
    }

  }
  async delHotel(hotel_id) {
    const { app } = this;
    const updateHotel = await app.model.Hotel.findOne({ where: { hotel_id } });

    if (updateHotel) {
      return await updateHotel.destroy();
    }
    return '该酒店不存在';

  }
  // 按照酒店价格进行升序排序
  async sortPrice() {
    const { app } = this;
    const result = await app.model.Hotel.findAll({
      order: [[ 'hotel_price', 'ASC' ]],
    });
    console.log(result);
    return result;
  }
}
module.exports = AdminService;
